using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEditor;
using UnityEngine;

public class Player : Entity
{
    [Header("Attack details")]
    public float[] attackMovement;
    public bool isBusy {  get; private set; }
    [Header("Move info")]
    public float MoveSpeed = 3.0f;
    public float JumpFoece = 10f;
    [Header("Dash info")]
    public float DashSpeed = 30.0f;
    public float DashExitSpeed = 10.0f;
    public float DashDuration = 0.2f;
    public float DashDir;

    public SkillManager skill { get; private set; }
    public GameObject sword { get; private set; }


    #region States
    public PlayerStateMachine stateMachine { get; private set; }
    public PlayerIdleState idleState { get; private set; }
    public PlayerMoveState moveState { get; private set; }
    public PlayerJumpState jumpState { get; private set; }
    public PlayerAirState airState { get; private set; }
    public PlayerDashState dashState { get; private set; }
    public PlayerWallSlideState wallSlideState { get; private set; }
    public PlayerWallJumpState wallJump { get; private set; }


    public PlayerAimSwordState aimSword {  get; private set; }
    public PlayerCatchSwordState catchSword { get; private set; }
    

    public PlayerPrimeryAttactState primeryAttackState {  get; private set; }
    protected override void Awake()
    {
        stateMachine = new PlayerStateMachine();
        idleState = new PlayerIdleState(this, stateMachine, "Idle");
        moveState = new PlayerMoveState(this, stateMachine, "Move");
        jumpState = new PlayerJumpState(this, stateMachine, "Jump");
        airState = new PlayerAirState(this, stateMachine, "Jump");
        dashState = new PlayerDashState(this, stateMachine, "Dash");
        wallSlideState = new PlayerWallSlideState(this, stateMachine, "WallSide");
        wallJump = new PlayerWallJumpState(this, stateMachine, "Jump");
        primeryAttackState = new PlayerPrimeryAttactState(this, stateMachine, "Attack");
        aimSword = new PlayerAimSwordState(this, stateMachine, "AimSword");
        catchSword = new PlayerCatchSwordState(this, stateMachine, "CatchSword");
    }
    protected override void Start()
    {
        base.Start();
        skill = SkillManager.instance;
        stateMachine.Initialize(idleState);


    }
    protected override void Update()
    {
        stateMachine.currentState.Update();

        DashInputCheck();

    }

    public void AssignNewSword(GameObject _newSword)
    {
        sword = _newSword;
    }

    public void ClearTheSword()
    {
        Destroy(sword);
    }

    public IEnumerator BusyFor(float _seconds)
    {
        isBusy = true;
        yield return  new WaitForSeconds(_seconds);
        isBusy = false;
    }
    #endregion
    public void AnimationTrigger() => stateMachine.currentState.AnimationFinishTriggle();
   
    
    public void DashInputCheck()
    {

        if (IsWallDetected())
        {
            return;
        }
        if (Input.GetKeyUp(KeyCode.LeftShift) && SkillManager.instance.dash.CanUseSkill())
        {
            DashDir = Input.GetAxisRaw("Horizontal");
            if (DashDir == 0)
            {
                DashDir = facingDir;
            }
            stateMachine.ChangeState(dashState);
        }
    }

    
}

